package com.app.controller;

import com.app.service.IHuibenClassService;
import com.app.vo.ClassInfoVO;
import com.app.vo.ClassDetailVO;
import com.app.vo.HomeworkInfoVO;
import com.app.vo.PunchStatsVO;
import com.app.vo.PunchItemVO;
import com.app.vo.ClassMemberVO;
import com.app.dto.CreateClassDTO;
import com.app.dto.JoinClassDTO;
import com.app.entity.po.HuibenClassMember;
import com.app.dto.ClassDetailRequestDTO;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import org.springframework.security.core.Authentication;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

@RestController
@RequestMapping("/app-api/class")
@Slf4j
public class HuibenClassController {

    @Autowired
    private IHuibenClassService huibenClassService;

    

    // 1. 获取用户班级列表
    @GetMapping("/list")
    public CommonResult<List<ClassInfoVO>> getClassList() {
        return success(huibenClassService.getClassList());
    }

    // 2. 获取班级详情
    @PostMapping("/detail")
    public CommonResult<ClassDetailVO> getClassDetail(@RequestBody ClassDetailRequestDTO dto) {
        return success(huibenClassService.getClassDetail(dto.getClassId()));
    }


    // // 4. 获取班级打卡统计
    // @GetMapping("/punch/stats")
    // public CommonResult<PunchStatsVO> getPunchStats(@RequestParam("classId") Long classId) {
    //     return success(huibenClassService.getPunchStats(classId));
    // }

    // 5. 获取班级打卡记录
    @GetMapping("/punch/list")
    public CommonResult<List<PunchItemVO>> getPunchList(@RequestParam("classId") Long classId,
                                                        @RequestParam("page") Integer page,
                                                        @RequestParam("pageSize") Integer pageSize) {
        return success(huibenClassService.getPunchList(classId, page, pageSize));
    }

    // 6. 获取班级成员列表
    @GetMapping("/member/list")
    public CommonResult<List<ClassMemberVO>> getMemberList(@RequestParam("classId") Long classId,
                                             @RequestParam("page") Integer page,
                                             @RequestParam("pageSize") Integer pageSize) {
        return success(huibenClassService.getMemberList(classId, page, pageSize));
    }

    // 7. 创建班级
    @PostMapping("/create")
    public CommonResult<Map<String, Object>> createClass(@RequestBody CreateClassDTO dto) {
        huibenClassService.createClass(dto);
        return success(null);
    }

    // 8. 加入班级
    @PostMapping("/join")
    public CommonResult<Map<String, Object>> joinClass(@RequestParam("inviteCode") String inviteCode) {
        log.info("用户加入班级，邀请码：{}",  inviteCode);
        Long classId = huibenClassService.joinClass(inviteCode);
        return success(Collections.singletonMap("classId", classId));
    }

    // 9. 检查是否为班级创建者
    @GetMapping("/check-creator")
    public CommonResult<Map<String, Boolean>> checkIsCreator(@RequestParam("classId") Long classId) {
        boolean isCreator = huibenClassService.checkIsCreator(classId);
        return success(Collections.singletonMap("isCreator", isCreator));
    }
    

}
